package com.jianbing.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by Han, Tixiang on 2016/8/16 0016.
 */
public class DateUtils {
    private static SimpleDateFormat yyyymmddFormat = new SimpleDateFormat("yyyy-MM-dd");

    private static SimpleDateFormat yyyymmddhhmmssFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat shebaoToolyyyymmddhhmmssFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static SimpleDateFormat shebaoToolyyyymmddFormat = new SimpleDateFormat("yyyy/MM/dd");
    public static String yyyymmddhhmmssFormat(Date date) {
        return yyyymmddhhmmssFormat.format(date);
    }

    public static String yyyymmddFormat(Date prevDate) {
        return yyyymmddFormat.format(prevDate);
    }

    public static boolean isSameDay(Date day1, Date day2) {
        String dayStr = yyyymmddFormat.format(day1);
        String today = yyyymmddFormat.format(day2);
        if (dayStr.equals(today)) {
            return true;
        }
        return false;
    }

    public static Date getDurationDate(Date date, int value, TimeUnit unit) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (unit == TimeUnit.DAYS) {
            calendar.add(Calendar.DAY_OF_MONTH, value);
        } else if (unit == TimeUnit.SECONDS) {
            calendar.add(Calendar.SECOND, value);
        } else if (unit == TimeUnit.MINUTES) {
            calendar.add(Calendar.MINUTE, value);
        } else if (unit == TimeUnit.HOURS) {
            calendar.add(Calendar.HOUR_OF_DAY, value);
        }
        Date dateResult = calendar.getTime();
        return dateResult;

    }

    public static Date getPreDate(String day) {
        Date date = null;
        try {
            date = yyyymmddFormat.parse(day);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date get(String day) {
        Date date = null;
        try {
            date = yyyymmddFormat.parse(day);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseYyyymmddDate(String dateStr) {
        try {
            return yyyymmddFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }
    public static Date parseShebaoToolYyyymmddDate(String dateStr) {
        try {
            return shebaoToolyyyymmddFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }
    public static Date parseYyyymmddhhmmssDate(String dateStr) {
        try {
            return yyyymmddhhmmssFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }


}
